package com.nanbei.orderService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanbei.commonUntil.JwtUtils;
import com.nanbei.commonUntil.R;
import com.nanbei.commonUntil.vo.orderVo.CourseWebVoOrder;
import com.nanbei.orderService.client.OrderCourseClient;
import com.nanbei.orderService.entity.Order;
import com.nanbei.orderService.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-14
 */
@RestController
@RequestMapping("/orderService/order")
@CrossOrigin  //解决跨域问题
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 点击购买  生成订单信息
     * @param courseId   课程ID
     * @param request
     * @return
     */
    @PostMapping("/getOrderId/{courseId}")
    public R getOrderId(@PathVariable String courseId, HttpServletRequest request){

        String orderId =  orderService.getOrderId(courseId, JwtUtils.getMemberIdByJwtToken(request));
        System.out.println(orderId+"------");

        return R.ok().data("orderId",orderId);
    }


    /**
     * 根据订单ID查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderInfoById/{orderId}")
    public R getOrderInfoById(@PathVariable String orderId){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_no",orderId);

        Order order = orderService.getOne(orderQueryWrapper);
        return R.ok().data("orderInfo",order);
    }


    /**
     * 根据课程ID和用户ID判断该用户是否购买该课程
     * @param courseId
     * @param centerId
     * @return
     */
    @GetMapping("/isNotBuy/{courseId}/{centerId}")
    public boolean isNotBuy(@PathVariable String courseId,@PathVariable String centerId){
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("course_id",courseId);
        orderQueryWrapper.eq("member_id",centerId);
        orderQueryWrapper.eq("status",1);
        int count = orderService.count(orderQueryWrapper);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

}

